<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown 文档阅读</title>
    <!-- 更换 CDN 链接引入 marked 库 -->
    <script src="https://unpkg.com/marked@4.3.0/marked.min.js"></script>
    <!-- 引入 highlight.js 库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            background-color: #f4f4f4;
        }

        #sidebar {
            width: 200px;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            position: fixed; /* 固定侧边栏位置 */
            height: calc(100vh - 40px); /* 使侧边栏高度为视口高度减去顶部和底部的间距 */
            overflow-y: auto; /* 允许侧边栏内容滚动 */
            top: 0; /* 确保侧边栏固定在顶部 */
            left: 0; /* 确保侧边栏固定在左侧 */
        }

        #markdown-content {
            flex: 1;
            background-color: white;
            padding: 20px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-left: 240px; /* 为侧边栏留出空间 */
        }

        pre code {
            display: block;
            background-color: black;
            color: white;
            padding: 10px;
            border-radius: 5px;
            position: relative;
        }

        .copy-button {
            position: absolute;
            top: 5px;
            right: 5px;
            background-color: #007bff;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
        }

        .copy-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <div id="sidebar">
        <!-- 大纲内容将在这里显示 -->
    </div>
    <div id="markdown-content"></div>
    <script>
        // 检查 marked 库是否加载成功
        if (typeof marked === 'undefined') {
            console.error('marked 库未成功加载，请检查引入的 CDN 链接或本地文件路径。');
        } else {
            // 使用 fetch API 加载本地 Markdown 文件
            fetch('TypeScript.md')
               .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.text();
                })
               .then(markdownText => {
                    // 将 Markdown 文本转换为 HTML
                    const htmlContent = marked.parse(markdownText);
                    const markdownContentDiv = document.getElementById('markdown-content');
                    markdownContentDiv.innerHTML = htmlContent;

                    // 应用语法高亮
                    document.querySelectorAll('pre code').forEach((block) => {
                        hljs.highlightBlock(block);

                        // 添加复制按钮
                        const copyButton = document.createElement('button');
                        copyButton.className = 'copy-button';
                        copyButton.textContent = '复制';
                        copyButton.onclick = () => {
                            navigator.clipboard.writeText(block.textContent).then(() => {
                                copyButton.textContent = '已复制';
                                setTimeout(() => {
                                    copyButton.textContent = '复制';
                                }, 2000);
                            });
                        };
                        block.parentNode.insertBefore(copyButton, block);
                    });

                    // 生成大纲
                    const headings = markdownContentDiv.querySelectorAll('h1, h2, h3, h4, h5, h6');
                    const sidebar = document.getElementById('sidebar');
                    headings.forEach(heading => {
                        const link = document.createElement('a');
                        link.href = `#${heading.id}`;
                        link.textContent = heading.textContent;
                        link.style.display = 'block';
                        link.style.marginLeft = `${(parseInt(heading.tagName[1]) - 1) * 10}px`;
                        sidebar.appendChild(link);
                    });
                })
               .catch(error => console.error('加载 Markdown 文件时出错:', error));
        }
    </script>
</body>

</html>